<template>
  <el-form label-width="100px">
    <el-form-item label="sku名称">
      <el-input placeholder="sku名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input
        placeholder="价格(元)"
        type="number"
        v-model="skuParams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input
        placeholder="重量(克)"
        type="number"
        v-model="skuParams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="sku描述">
      <el-input
        placeholder="sku描述"
        type="textarea"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true">
        <!-- index -->
        <el-form-item
          :label="item.attrName"
          v-for="item in attrArr"
          :key="item.id"
        >
          <el-select v-model="item.attrIdAndValueId">
            <!--  index -->
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <!-- index -->
        <el-form-item
          :label="item.saleAttrName"
          v-for="item in saleArr"
          :key="item.id"
        >
          <el-select v-model="item.saleIdAndValueId">
            <!-- index -->
            <el-option
              :value="`${item.id}:${saleArrValue.id}`"
              v-for="saleArrValue in item.spuSaleAttrValueList"
              :key="saleArrValue.id"
              :label="saleArrValue.saleAttrValueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imageArr" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <!-- $index  -->
          <template #="{ row }">
            <img :src="row.imgUrl" style="width: 100px; height: 100px" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <!--  $index  -->
          <template #="{ row }">
            <el-button type="primary" size="small" @click="handler(row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="primary" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
//引入请求api
import { reqAttr } from '@/api/product/attr'
import {
  reqSpuImageList,
  reqSpuHasSaleAttr,
  reqAddSku,
} from '@/api/product/spu'
import { ref, reactive } from 'vue'
import type { SkuData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'
//平台属性
let attrArr = ref<any>([])
//销售属性
let saleArr = ref<any>([])
//图片数据
let imageArr = ref<any>([])

//获取table组件实例vc
let table = ref<any>()
//收集sku参数
let skuParams = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',

  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '',
})
//自定义事件方法
let $emit = defineEmits(['changeScene'])

//取消按钮回调
const cancel = () => {
  $emit('changeScene', { flag: 0, params: '' })
}

//当前子组件方法对外暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  spuForm: any,
) => {
  //收集数据
  skuParams.category3Id = spuForm.category3Id
  skuParams.spuId = spuForm.id
  skuParams.tmId = spuForm.tmId
  //获取平台属性
  let result: any = await reqAttr(c1Id, c2Id, spuForm.category3Id)
  //获取对应的销售属性
  let result1: any = await reqSpuHasSaleAttr(spuForm.id)
  //获取照片墙数据
  let result2: any = await reqSpuImageList(spuForm.id)
  //平台属性
  attrArr.value = result.data
  //销售属性
  saleArr.value = result1.data
  //图片
  imageArr.value = result2.data
}

//设置默认图片的方法回调
const handler = (row: any) => {
  //点击时，全部的复选框不勾选
  imageArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  })
  table.value.toggleRowSelection(row, true)

  skuParams.skuDefaultImg = row.imgUrl
}

defineExpose({ initSkuData })

//保存按钮的方法
const save = async () => {
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      prev.push({
        attrId,
        valueId,
      })
    }
    return prev
  }, [])
  skuParams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
        prev.push({
          saleAttrId,
          saleAttrValueId,
        })
      }
      return prev
    },
    [],
  )

  //添加sku请求
  let result: any = await reqAddSku(skuParams)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加成功',
    })
    //通知父组件切换场景
    $emit('changeScene', { flag: 0, params: '' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加失败',
    })
  }
}
</script>

<style scoped></style>
